<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<c:set var="ctx" value="${pageContext.request.contextPath}" />

<link href="${ctx}/static/css/TreeGrid.css" rel="stylesheet" type="text/css"/>
<style>
    #treeTable{
        border-radius: 4px;
        overflow: hidden;
        border:1px solid #ddd;
    }
    .TreeGrid td{
        padding: 8px 3px;
    }
    .TreeGrid{
        border:none;
    }
    .TreeGrid .header{
        background:  rgba(255,255,255, .3);
    }
    .TreeGrid .row_active{
        background: rgba(255,255,255, .3);
    }
    .TreeGrid .row_hover{
        background:  rgba(255,255,255, .3);
    }
    .TreeGrid td{
        border:none;
        border-bottom:1px solid #ddd;
    }
    .image_hand{
        cursor: pointer;
    }

    .xiugai1{
        margin-left: 50px;
        margin-right: 15px;
        color: #dcbf29;
    }
</style>


<ul class="breadcrumb panel">
    <li><a href="javascript:backMainFun();"><i class="fa fa-home"></i> 首页</a></li>
    <li class="active">组织机构</li>
</ul>

<div class="row">
    <div class="col-xs-12">
        <div class="panel">
            <div class="panel-heading"  style="background: rgb(223,227,254);text-shadow: none; color: #333">
                组织机构树
            </div>
            <div class="panel-body" style="background: rgb(223,227,254);text-shadow: none;">
                <button type="button" class="btn btn-info" onclick="loadFun('/org/toOrgAdd')"><i class="glyphicon glyphicon-plus"></i> 新增组织</button>
                <div id="treeApp" class="ztree" style="height: 640px;overflow: auto;margin-top: 15px"></div>
            </div>
        </div>
    </div>
</div>

<script>
    //   部门组织树及选择部门代码
    var zTreeObj;
    var zNodes;
    function  filter(treeId, parentNode, data) {
        if (!data) return null;
        var childNodes = [];
        childNodes = data;

        for(var i=0;i<childNodes.length;i++){
            childNodes[i].isParent = true;
        };

        return childNodes;
    };

    function addDiyDom(treeId, treeNode) {
        var aObj = $("#" + treeNode.tId + "_a");
        if ($("#diyBtn_"+treeNode.org_id).length>0){
            return;
        };
        var editStr = "<span type='button' class='xiugai1 text-warning' id='xiugai_" + treeNode.org_id + "'>修改</span> <span type='button' id='shanchu_" + treeNode.org_id + "' class='shanchu1 text-danger'>删除</span>";
        aObj.append(editStr);
        var btn = $("#xiugai_"+treeNode.org_id);
        if (btn) btn.bind("click", function(){
            updateFun(treeNode.org_id);
        });

        var btn2 = $("#shanchu_"+treeNode.org_id);
        if (btn2) btn2.bind("click", function(){
            deleteFun(treeNode.org_id);
        });
    };


    $.post('${ctx}/org/getOrgTree',{
        parentId:'',
    },function (data) {
        var setting = {
            view: {
                showLine: false,
                expandSpeed:'fast',
                addDiyDom: addDiyDom
            },
            async: {
                enable: true,
                contentType: "application/x-www-form-urlencoded",
                url: "${ctx}/org/getOrgTree",
                dataType: "text",
                type:'post',
                autoParam: ["org_id=parentId"],
                dataFilter: filter
            },
            check:{
                enable: false,
                chkStyle: "radio",
                radioType: "all"
            }
        };

        zNodes = data;
        zNodes[0].open=true;
        zNodes[0].isParent = true;

        for(var i=0;i<zNodes[0].children.length;i++){
            zNodes[0].children[i].isParent = true;
        };

        zTreeObj = $.fn.zTree.init($("#treeApp"), setting, zNodes);
    });
    //部门组织树及选择部门代码 end


    function updateFun(value){
        loadFun("/org/toOrgUpdate?org_id="+value);
    }
    function deleteFun(value){
        zConfirm('确认删除？');
        confirmOk.click(function(){
            $.post("${ctx}/org/deleteOrg?org_id="+value,function(data){
                zConfirmHide();
                if(data.success){
                    zAlert(data.msg)
                    setTimeout(function(){
                        loadFun('/org/toOrgIndex');
                    },100);
                }else{
                    setTimeout(function(){
                        zAlert(data.msg)
                    },100);
                }
            });
        });
    }
</script>

